{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### numpy的速度要比纯python要快"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[     0      1      2 ..., 999997 999998 999999]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "my_arr = np.arange(1000000)\n",
    "my_list = list(range(1000000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 35.9 ms\n"
     ]
    }
   ],
   "source": [
    "%time for _ in range(10):my_arr2 = my_arr * 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 1.16 s\n"
     ]
    }
   ],
   "source": [
    "%time for _ in range(10):my_list2 = [x * 2 for x in my_list]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### numpy的ndarry：一种多维数组对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1.25298716 -1.7343435  -0.00697016]\n",
      " [-0.52350676 -0.67632315  0.72901536]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "data = np.random.randn(2, 3)# 生成（2,3）随机数组\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "data * 10: \n",
      " [[ 12.52987164 -17.34343498  -0.06970156]\n",
      " [ -5.23506758  -6.76323147   7.29015358]]\n",
      "data + data: \n",
      " [[ 2.50597433 -3.468687   -0.01394031]\n",
      " [-1.04701352 -1.35264629  1.45803072]]\n"
     ]
    }
   ],
   "source": [
    "print('data * 10: \\n', data * 10)# 数学运算\n",
    "print('data + data: \\n', data + data)# 和自身相加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "data shape: (2, 3)\n",
      "data dtype: float64\n"
     ]
    }
   ],
   "source": [
    "print('data shape:', data.shape)\n",
    "print('data dtype:', data.dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[6, 7.5, 8, 0, 1]\n"
     ]
    }
   ],
   "source": [
    "data1 = [6, 7.5, 8, 0, 1]\n",
    "arr1 = np.array(data1)# 生成ndarray\n",
    "print(data1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "arr2: [[1 2 3 4]\n",
      " [5 6 7 8]]\n",
      "arr2.ndim 2\n",
      "arr2.shape (2, 4)\n"
     ]
    }
   ],
   "source": [
    "data2 = [[1, 2, 3, 4], [5, 6, 7, 8]]\n",
    "arr2 = np.array(data2)\n",
    "print('arr2:',arr2)\n",
    "print('arr2.ndim',arr2.ndim)\n",
    "print('arr2.shape',arr2.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]\n",
      "[[ 0.  0.  0.  0.  0.  0.]\n",
      " [ 0.  0.  0.  0.  0.  0.]\n",
      " [ 0.  0.  0.  0.  0.  0.]]\n",
      "[[[  1.61272725e-312   0.00000000e+000]\n",
      "  [  3.62633684e+228   8.82087105e+199]\n",
      "  [  6.79000872e+180   2.09473745e-110]]\n",
      "\n",
      " [[  8.29655075e-114   1.09936966e+248]\n",
      "  [  2.09666503e-110   6.48224638e+170]\n",
      "  [  3.67145870e+228   1.16263363e+295]]]\n"
     ]
    }
   ],
   "source": [
    "print(np.zeros(10))\n",
    "print(np.zeros((3, 6)))\n",
    "print(np.empty((2, 3, 2)))# 用empty是一对未初始化垃圾值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]\n"
     ]
    }
   ],
   "source": [
    "print(np.arange(15))# arange是range数组版"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "float64\n",
      "int32\n"
     ]
    }
   ],
   "source": [
    "arr1 = np.array([1, 2, 3], dtype=np.float64)# 转换时指定dtype类型\n",
    "arr2 = np.array([1, 2, 3], dtype=np.int32)\n",
    "\n",
    "print(arr1.dtype)\n",
    "print(arr2.dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "int32\n",
      "[ 1.  2.  3.  4.  5.]\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([1, 2, 3,  4, 5])\n",
    "print(arr.dtype)\n",
    "\n",
    "float_arr = arr.astype(np.float64)# 转换dtype类型\n",
    "print(float_arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9.]\n"
     ]
    }
   ],
   "source": [
    "int_array = np.arange(10)\n",
    "calibers = np.array([.22, .270, .357, .380, .44, .50], dtype=np.float64)\n",
    "print(int_array.astype(calibers.dtype))# 根据已知dtype转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ True False  True]\n",
      " [False  True False]]\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([[1., 2., 3.], [4., 5., 6.]])\n",
    "arr2 = np.array([[0., 4., 1.], [7., 2., 12.]])\n",
    "\n",
    "print(arr > arr2)# 比较结果返回布尔值数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[[  0.   0.   0.]\n",
      " [ 10.  10.  10.]\n",
      " [ 20.  20.  20.]\n",
      " [ 30.  30.  30.]]\n",
      "\n",
      "第二个数组：\n",
      "[ 1.  2.  3.]\n",
      "\n",
      "第一个数组加第二个数组：\n",
      "[[  1.   2.   3.]\n",
      " [ 11.  12.  13.]\n",
      " [ 21.  22.  23.]\n",
      " [ 31.  32.  33.]]\n"
     ]
    }
   ],
   "source": [
    "# 广播性质\n",
    "import numpy as np\n",
    "a = np.array([[0.0,0.0,0.0],[10.0,10.0,10.0],[20.0,20.0,20.0],[30.0,30.0,30.0]]) \n",
    "b = np.array([1.0,2.0,3.0])  \n",
    "print ('第一个数组：')  \n",
    "print (a) \n",
    "print ('\\n第二个数组：')  \n",
    "print (b) \n",
    "print ('\\n第一个数组加第二个数组：')  \n",
    "print (a + b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Bob' 'Joe' 'Will' 'Bob' 'Will' 'Joe' 'Joe']\n",
      "[[-0.2199931   3.06142775 -0.07026789  0.20966294]\n",
      " [ 1.30706537  2.18373408  0.70619482 -0.21398887]\n",
      " [-1.07471922 -0.34156791 -0.30165979 -0.62626162]\n",
      " [-0.79452106 -1.12769374  0.5356514   0.82808898]\n",
      " [ 0.53364938 -0.39252664 -0.89327254 -0.60239005]\n",
      " [-0.34979571  1.50331541  0.467521    0.12255861]\n",
      " [ 0.85094207 -0.24210178 -0.984189    0.54836486]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'])\n",
    "data = np.random.randn(7, 4)\n",
    "\n",
    "print(names)\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ True False False  True False False False]\n",
      "[[-0.2199931   3.06142775 -0.07026789  0.20966294]\n",
      " [-0.79452106 -1.12769374  0.5356514   0.82808898]]\n",
      "[False  True  True False  True  True  True]\n",
      "[[ 1.30706537  2.18373408  0.70619482 -0.21398887]\n",
      " [-1.07471922 -0.34156791 -0.30165979 -0.62626162]\n",
      " [ 0.53364938 -0.39252664 -0.89327254 -0.60239005]\n",
      " [-0.34979571  1.50331541  0.467521    0.12255861]\n",
      " [ 0.85094207 -0.24210178 -0.984189    0.54836486]]\n"
     ]
    }
   ],
   "source": [
    "# 布尔型索引\n",
    "print(names=='Bob')\n",
    "print(data[names=='Bob'])\n",
    "print(names!='Bob')\n",
    "print(data[~(names=='Bob')])# 使用~进行反转"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ True False  True  True  True False False]\n",
      "[[-0.2199931   3.06142775 -0.07026789  0.20966294]\n",
      " [-1.07471922 -0.34156791 -0.30165979 -0.62626162]\n",
      " [-0.79452106 -1.12769374  0.5356514   0.82808898]\n",
      " [ 0.53364938 -0.39252664 -0.89327254 -0.60239005]]\n"
     ]
    }
   ],
   "source": [
    "mask = (names=='Bob') | (names=='Will')\n",
    "print(mask)\n",
    "print(data[mask])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4]\n",
      " [ 5  6  7  8  9]\n",
      " [10 11 12 13 14]]\n",
      "[[ 0  5 10]\n",
      " [ 1  6 11]\n",
      " [ 2  7 12]\n",
      " [ 3  8 13]\n",
      " [ 4  9 14]]\n"
     ]
    }
   ],
   "source": [
    "import  numpy as np\n",
    "\n",
    "arr = np.arange(15).reshape((3, 5))\n",
    "print(arr)\n",
    "print(arr.T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.26783317 -0.01280273  0.18230101]\n",
      " [ 0.36741342 -1.60260926  0.78392119]\n",
      " [-0.82735806  0.00953338 -0.55083312]\n",
      " [ 0.21257818  1.41354988 -0.19956925]\n",
      " [ 1.12739284  1.30390088  1.23858616]\n",
      " [-0.69865366  0.06837629  1.11606121]]\n",
      "[[ 0.10513218  0.0650219   0.12105445 -0.11141446 -0.09285118  0.38970631]\n",
      " [ 0.0650219   3.3178815  -0.7510705  -2.34371062 -0.70447042  0.50862883]\n",
      " [ 0.12105445 -0.7510705   0.98802937 -0.052473   -1.60258125 -0.03607489]\n",
      " [-0.11141446 -2.34371062 -0.052473    2.08314063  1.83560434 -0.27459672]\n",
      " [-0.09285118 -0.70447042 -1.60258125  1.83560434  4.5052678   0.68383673]\n",
      " [ 0.38970631  0.50862883 -0.03607489 -0.27459672  0.68383673  1.73838487]]\n"
     ]
    }
   ],
   "source": [
    "arr = np.random.randn(6, 3)\n",
    "print(arr)\n",
    "print(np.dot(arr, arr.T))# 计算矩阵内积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]]\n",
      "\n",
      " [[ 8  9 10 11]\n",
      "  [12 13 14 15]]]\n",
      "----------------------------\n",
      "[[[ 0  1  2  3]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[ 4  5  6  7]\n",
      "  [12 13 14 15]]]\n"
     ]
    }
   ],
   "source": [
    "# 对于高维数组，transpose需要得到一个由编号组成的元组才能对这些轴进行转置\n",
    "arr = np.arange(16).reshape((2, 2, 4))\n",
    "print(arr)\n",
    "print('----------------------------')\n",
    "print(arr.transpose((1, 0, 2)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]]\n",
      "\n",
      " [[ 8  9 10 11]\n",
      "  [12 13 14 15]]]\n",
      "----------------------------\n",
      "[[[ 0  4]\n",
      "  [ 1  5]\n",
      "  [ 2  6]\n",
      "  [ 3  7]]\n",
      "\n",
      " [[ 8 12]\n",
      "  [ 9 13]\n",
      "  [10 14]\n",
      "  [11 15]]]\n"
     ]
    }
   ],
   "source": [
    "print(arr)\n",
    "print('----------------------------')\n",
    "print(arr.swapaxes(1, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "[ 0.          1.          1.41421356  1.73205081  2.          2.23606798\n",
      "  2.44948974  2.64575131  2.82842712  3.        ]\n",
      "[  1.00000000e+00   2.71828183e+00   7.38905610e+00   2.00855369e+01\n",
      "   5.45981500e+01   1.48413159e+02   4.03428793e+02   1.09663316e+03\n",
      "   2.98095799e+03   8.10308393e+03]\n"
     ]
    }
   ],
   "source": [
    "arr = np.arange(10)\n",
    "\n",
    "print(arr)\n",
    "print(np.sqrt(arr))\n",
    "print(np.exp(arr))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.08971497  0.57169441 -0.16575108 -1.93084801 -0.89761078  0.30901905\n",
      "  0.2393505  -0.10147055]\n"
     ]
    }
   ],
   "source": [
    "x = np.random.randn(8)\n",
    "y = np.random.randn(8)\n",
    "\n",
    "print(np.minimum(x, y))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-1.09689645  2.21487853  0.0494108   0.17230215]\n",
      " [ 0.05167529 -2.10384902 -1.50898636  1.01575317]\n",
      " [ 2.79523206  0.57146598  1.93840667  0.09440613]\n",
      " [ 0.11831827  0.50731095  2.65976932 -0.72799341]]\n",
      "[[False  True  True  True]\n",
      " [ True False False  True]\n",
      " [ True  True  True  True]\n",
      " [ True  True  True False]]\n",
      "[[-2  2  2  2]\n",
      " [ 2 -2 -2  2]\n",
      " [ 2  2  2  2]\n",
      " [ 2  2  2 -2]]\n"
     ]
    }
   ],
   "source": [
    "arr = np.random.randn(4, 4)\n",
    "print(arr)\n",
    "print(arr>0)\n",
    "print(np.where(arr>0, 2, -2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
